// index/pages/psychological/psychological.js
Page({
    data: {
      loadingMore: false,
      hasMore: true,
      page: 1,
      pageSize: 8,
      list: [],
    },
  
    onLoad: function () {
      this.setData({
        list: [],
        page: 1,
        hasMore: true,
      });
      this.loadData();
    },
  
    // 加载数据（原有逻辑不变）
    loadData: function (callback) {
      if (this.data.loadingMore || !this.data.hasMore) {
        if (callback && typeof callback === 'function') {
          callback();
        }
        return;
      }
  
      this.setData({ loadingMore: true });
  
      wx.request({
        url: "http://localhost:8080/mindKnowledge/page",
        data: {
          pageNum: this.data.page,
          pageSize: this.data.pageSize
        },
        success: (resp) => {
          console.log("分页数据响应：", resp);
          const pageData = resp.data;
  
          if (pageData && Array.isArray(pageData.list)) {
            const currentPageData = pageData.list;
            const total = pageData.total || 0;
            const { list } = this.data;
            const newList = list.concat(currentPageData);
  
            this.setData({
              list: newList,
              loadingMore: false,
              page: this.data.page + 1,
              hasMore: newList.length < total
            });
          } else {
            wx.showToast({ title: '数据格式错误', icon: 'none' });
            this.setData({ loadingMore: false });
          }
  
          if (callback && typeof callback === 'function') {
            callback();
          }
        },
        fail: (err) => {
          console.error("请求失败：", err);
          wx.showToast({ title: '加载失败', icon: 'none' });
          this.setData({ loadingMore: false });
          
          if (callback && typeof callback === 'function') {
            callback();
          }
        }
      });
    },
  
    // 新增：列表项点击跳转至psyd页面
    goToPsyd: function (e) {
      // 获取当前点击项的id（从data-id中获取）
      const itemId = e.currentTarget.dataset.id;
      // 跳转至psyd页面，并携带id参数
      wx.navigateTo({
        url: `/index/pages/psyd/psyd?id=${itemId}`
      });
    },
  
    // 上拉触底加载更多（原有逻辑不变）
    onReachBottom: function () {
      console.log("触发上拉触底，加载更多");
      this.loadData();
    },
  
    // 下拉刷新（原有逻辑不变）
    onPullDownRefresh: function () {
      this.setData({
        list: [],
        page: 1,
        hasMore: true
      });
      this.loadData(() => {
        wx.stopPullDownRefresh();
      });
    },
  
    // 其他生命周期函数（保持不变）
    onReady() {},
    onShow() {},
    onHide() {},
    onUnload() {},
    onShareAppMessage() {},
  });